﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Peak.Core;
using Peak.Core.DataInterfaces;
using Peak.Resources;
using Peak.Web.Controllers.Services;
using OpenFlashChart;
using MvcContrib;

namespace Peak.Web.Controllers
{
  public class WorkHistoryController : BaseController
  {
    private readonly ITaskRepository _taskRepository; 
    private readonly ISprintRepository _sprintRepository;

    public WorkHistoryController(ISessionService sessionService, ITaskRepository taskRepository, ISprintRepository sprintRepository)
      : base(sessionService)
    {      
      _taskRepository = taskRepository;
      _sprintRepository = sprintRepository;
    }    

    public ActionResult Current()
    {
      Sprint sprint = _sessionService.Project.CurrentSprint;
      if (sprint == null)
      {
        return MessageBox(Messages.NoCurrentSprint);
      }
      else
      {        
        return this.RedirectToAction(f => f.Index(sprint.Id));
      }
    }

    public ActionResult Index(int id)
    {
      return View(id);
    }

    public ActionResult Chart(int id)
    {
      Project project = _sessionService.Project;      
      Page<Task> page = _taskRepository.GetPage(new TaskFilter() { ProjectId = project.Id, AssignedTo = _sessionService.User.UserName, SprintId = id });
      DateTime toDate = DateTime.Now.Date;

      Sprint sprint = _sprintRepository.Get(id);

      DateTime start = sprint.StartDate;
      DateTime end = sprint.EndDate.Date;
      if(end > toDate)
        end = toDate;

      List<string> labels = new List<string>();
      Dictionary<DateTime, int> dateToInt = new Dictionary<DateTime, int>();
      int i = 0;
      for (DateTime date = start.Date; date <= end; date = date.AddDays(1))
      {
        if (project.Calendar.IsWorkingDay(date))
        {
          labels.Add(date.Day.ToString());
          dateToInt[date] = i;
          i++;
        }
      }

      OpenFlashChart.OpenFlashChart chart = new OpenFlashChart.OpenFlashChart();
      HBar bar = new HBar();
      bar.Colour = "#2A343D";

      List<string> taskNames = new List<string>();
      foreach (Task task in page.Results)
      {        
        if(task.StartDate != null)
        {
          int s = 0;
          if(task.StartDate >= start)
          {
            s = dateToInt[task.StartDate.Value.Date];
          }
          int e = dateToInt.Count - 1;
          if(task.EndDate < end)
          {
            e = dateToInt[end];
          }
          if(task.EndDate != null && task.EndDate < end)
            e = dateToInt[task.EndDate.Value];

          bar.Values.Add(new HBarValue(s, e + 1, task.Name, "redirect('http://localhost:2259/ScrumPeak/Task/Index?taskId="+task.Id+"')"));
          taskNames.Add(task.Name);
        }
      }      
      chart.AddElement(bar);
      chart.Tooltip = new ToolTip("_default");
      chart.Tooltip.MouseStyle = ToolTipStyle.NORMAL;
 
      chart.Title.Text = "Work history";
      chart.Bgcolor = "#E7E9DC";            

      chart.X_Axis.GridColour = "BBBEAB";     
      chart.X_Axis.Colour = "172028";
      chart.X_Axis.Offset = false;
      chart.X_Axis.SetLabels(labels);
      
      chart.Y_Axis.GridColour = "BBBEAB";
      chart.Y_Axis.Colour = "172028";
      //chart.Y_Axis.Offset = false;           
      
      chart.Y_Axis.SetLabels(new string[]{});
      chart.Y_Axis.Min = 1;
      chart.Y_Axis.Max = taskNames.Count;      
                 
      return new ChartDataResult(chart); 
    }
  }
}
